package com.auerdi.ui.fragment.a_home.adapter.delegate;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;

import com.auerdi.GlideApp;
import com.auerdi.R;
import com.auerdi.ui.detail.ADProductDetailFragment;
import com.auerdi.ui.detail.ADProductWebviewFragment;
import com.auerdi.ui.fragment.a_home.adapter.items.HomeItemsBean;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.retrofit.beans.banner.HomeBanner;
import com.example.egghttp.retrofit.beans.goods.Product;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by zhy on 16/6/22.
 */
public class AisBanner implements ItemViewDelegate<HomeItemsBean> {


    @Override
    public int getItemViewLayoutId() {
        return R.layout.item_home_a_banner;
    }

    @Override
    public boolean isForViewType(HomeItemsBean item, int position) {

//        item.getRouter()
        return item.getRouter().equals(getClass().getSimpleName());
//        return item.getRouter().equals(AisBanner.class.getSimpleName());
    }

    @Override
    public void convert(ViewHolder holder, HomeItemsBean item, int position) {


        Banner banner = holder.getView(R.id.banner);
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                HomeBanner banner = (HomeBanner) path;



                GlideApp.with(holder.getConvertView().getContext())
                        .load(banner.getImg())
                        .placeholder(R.mipmap.eptb)
                        .error(R.mipmap.eptb)
                        .fitCenter()
                        .into(imageView);

                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        L.d("hcia", "banner.getUrl():" + banner.getUrl());


                        Observable.just(banner.getUrl())
                                .map(new Func1<String, Uri>() {
                                    @Override
                                    public Uri call(String s) {
                                        return Uri.parse(s);
                                    }
                                })
                                .map(new Func1<Uri, String[]>() {
                                    @Override
                                    public String[] call(Uri uri) {
                                        return uri.getQuery().split("/");
                                    }
                                })
                                .doOnNext(new Action1<String[]>() {
                                    @Override
                                    public void call(String[] strings) {
                                        String productId = strings[4];
                                        String tid = strings[6].split("\\.")[0];
                                    }
                                })

                                .subscribe(new Subscriber<String[]>() {
                                    @Override
                                    public void onCompleted() {
                                        L.d("hcia", "onCompleted:");

                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        L.d("hcia", "e:" + e);
                                        EventBusActivityScope.getDefault((Activity) holder.getConvertView().getContext()).post(ADProductWebviewFragment.newInstance(banner));

                                    }

                                    @Override
                                    public void onNext(String[] strings) {
                                        L.d("hcia", "strings:" + strings.length);
                                        String productId = strings[4];
                                        String tid = strings[6].split("\\.")[0];

                                        Product product = new Product();
                                        product.setId(productId);
                                        product.setHuodongType(tid);
                                        EventBus.getDefault().post(ADProductDetailFragment.newInstance(product));

                                    }
                                });


                    }
                });


            }
        });

        List<HomeBanner> ee = (List<HomeBanner>) item.getItem();
        banner.setImages(ee);
        banner.start();


    }
}
